package org.ywf.common.entity;

import java.io.Serializable;

/**
 * 链式调用-建造者模式
 * 优点：
 *      1.可以分步创建对象， 暂缓创建步骤或递归运行创建步骤。
 *      2.生成不同形式的产品时， 可以复用相同的制造代码。
 *      3.单一职责原则。 可以将复杂构造代码从产品的业务逻辑中分离出来。
 * 缺点：
 *      由于该模式需要新增多个类，因此代码整体复杂程度会有所增加。
 *
 * @Author YWF
 * @Date 2023/3/16 15:47
 */
public class Student implements Serializable {

    //整整的属性是不可变的
    private final String studentId;
    private final String studengName;
    private final Integer studentAge;
    private final String studentBirthday;
    private final String studentAddress;

    //私有构造方法，只被 Builder 类调用
    private Student(Builder builder) {
        this.studentId = builder.studentId;
        this.studengName = builder.studengName;
        this.studentAge = builder.studentAge;
        this.studentBirthday = builder.studentBirthday;
        this.studentAddress = builder.studentAddress;
    }

    public static class Builder{

        //必须的参数
        private String studentId;
        private String studengName;

        //可选的参数
        private Integer studentAge;
        private String studentBirthday;
        private String studentAddress;

        public Builder(String studentId, String studengName) {
            this.studentId = studentId;
            this.studengName = studengName;
        }

        //使用设置好的参数值新建 Student 对象
        public Student build(){
            return new Student(this);
        }

        public Builder setStudentAge(Integer studentAge) {
            this.studentAge = studentAge;
            return this;
        }

        //每个 setter 方法都返回当前的对象，做到链式调用
        public Builder setStudentBirthday(String studentBirthday) {
            this.studentBirthday = studentBirthday;
            return this;
        }

        public Builder setStudentAddress(String studentAddress) {
            this.studentAddress = studentAddress;
            return this;
        }
    }

    public String getStudentId() {
        return studentId;
    }

    public String getStudengName() {
        return studengName;
    }

    public Integer getStudentAge() {
        return studentAge;
    }

    public String getStudentBirthday() {
        return studentBirthday;
    }

    public String getStudentAddress() {
        return studentAddress;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentId='" + studentId + '\'' +
                ", studengName='" + studengName + '\'' +
                ", studentAge=" + studentAge +
                ", studentBirthday='" + studentBirthday + '\'' +
                ", studentAddress='" + studentAddress + '\'' +
                '}';
    }

    /**
     * 测试方法
     **/
    public static void main(String[] args) {
        Student st = new Student.Builder("100001", "张三")
                .setStudentAddress("兰州市安宁区")
                .setStudentAge(24)
                .setStudentBirthday("2023-02-23")
                .build();
        System.out.println("st:"+st);
    }


}
